<?php

/**
 * Program filter form.
 *
 * @package    sf_sandbox
 * @subpackage filter
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormFilterTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class ProgramFormFilter extends BaseProgramFormFilter {

    public function configure() {
        $this->widgetSchema['end_date'] = new sfWidgetFormFilterDate(array('from_date' => new sfHijreeDateWidget(), 'to_date' => new sfHijreeDateWidget(),
                    'template' => I18nUtilities::__('From') . ' %from_date%<br/>' . I18nUtilities::__('To') . ' %to_date%',
                    'with_empty' => false));

        $this->widgetSchema['start_date'] = new sfWidgetFormFilterDate(array('from_date' => new sfHijreeDateWidget(), 'to_date' => new sfHijreeDateWidget(),
                    'template' => I18nUtilities::__('From') . ' %from_date%<br/>' . I18nUtilities::__('To') . ' %to_date%',
                    'with_empty' => false));

        $this->widgetSchema['is_mandatory'] = new sfWidgetFormChoice(array('choices' => array('' => '', 0 => 'Optional', 1 => 'Mandatory')));
        $this->validatorSchema['is_mandatory'] = new sfValidatorChoice(array('choices' => array(0, 1), 'required' => false));

        $this->widgetSchema->setLabels(array(
            'name' => 'Program name',
            'program_code' => 'Program code',
            'Trainer' => 'Trainer',
            'is_mandatory' => 'Mandatory?',
            'training_timing_type' => 'Training Time',
            'duration_days' => 'Program duration in days',
            'duration_hours' => 'Program duration in hours',
            'start_date' => 'Training start date',
            'end_date' => 'Training end date',
            'location_of_holding' => 'Location held',
            'target_number' => 'Target participant number',
            'updated_at' => 'Last update date',
            'created_at' => 'Creation date',
            'code_target_list' => 'Target categories'
        ));

        // --------------

        $user = sfContext::getInstance()->getUser();
        if ($user->hasAttribute('kts_global_program_filter') && is_array($data = $user->getAttribute('kts_global_program_filter'))) {
            unset($this['start_date']);
        }
        if ($user->hasAttribute('program_type') && $user->getAttribute('program_type') == 'night')
            unset($this['is_mandatory']);

        // --------------
    }

    public function getFields() {
        return array(
            'id' => 'Number',
            'name' => 'Text',
            'program_code' => 'Text',
            'duration_days' => 'Number',
            'duration_hours' => 'Number',
            'start_date' => 'Date',
            'end_date' => 'Date',
            'trainer_id' => 'ForeignKey',
            'location_of_holding' => 'ForeignKey',
            'is_mandatory' => 'Enum',
            'training_timing_type' => 'Number',
            'target_number' => 'Number',
            'notes' => 'Text',
            'created_at' => 'Date',
            'updated_at' => 'Date',
            'created_by_user_id' => 'ForeignKey',
            'updated_by_user_id' => 'ForeignKey',
            'teacher_list' => 'ManyKey',
            'code_target_list' => 'ManyKey',
            'code_sector_list' => 'ManyKey',
        );
    }

    public function doBuildQuery(array $values) {
        $user = sfContext::getInstance()->getUser();

        $q = parent::doBuildQuery($values);

        if ($user->hasAttribute('program_type'))
            $q->addWhere('r.global_program_type = ?', $user->getAttribute('program_type'));

        // --------------


        if ($user->hasAttribute('kts_global_program_filter') && is_array($data = $user->getAttribute('kts_global_program_filter'))) {
            $semister_map = array(1 => 'first', 2 => 'second');
            $q->addWhere('semester = ?', $semister_map[$data['semester']]);
            $q->addWhere('academic_year_id = ?', $data['year']);
        }

        // --------------

        return $q;
    }

    public function doBuildQueryForReport($values) {
        $q = parent::doBuildQuery($values);
//        if ($values['intg_identity'] != '' || $values['is_manager'] != '' || $values['school_intg_id'] != '') {
//            $q->innerJoin('r.Teacher t');
//        }

        if ($values['intg_identity']) {
            $q->addWhere('t.intg_identity = ?', $values['intg_identity']);
        }

        if ($values['is_manager']) {
            $q->addWhere('t.is_manager = ?', $values['is_manager']);
        }

        if ($values['school_intg_id']) {
            $q->addWhere('t.school_intg_id = ?', $values['school_intg_id']);
        }

//        $q->leftJoin('r.LocationsInSector location');
        $q->leftJoin('r.CodeTarget target');
        $q->leftJoin('r.CodeSector sector');
        $q->leftJoin('r.LinkTeacherProgram link ON r.id = link.program_id');
//        $q->addWhere('link.assign_status = 2');

        $q->leftJoin('r.Trainer t');

        $select_str = <<<EOS
r.name as program_name
, CONCAT(t.f_name," ",t.s_name," ",t.t_name," ",t.l_name) as trainer_name
, r.is_mandatory
, r.start_date
, r.end_date
, r.location_of_holding
, target.title
, sector.name
, link.assign_status
EOS;


        $q->select($select_str);
        $q->orderBy('r.name ASC');
        return $q;
    }

}
